import { positionCreateSchema } from "~/validators/position";

let schema = PositionSchema;

export default defineEventHandler(async (event) => {
  const result = await runValidate(positionCreateSchema, event);
  const { name } = result.data;
  // findOne 是 Mongoose 的方法，用于查找符合条件的第一条数据。
  const oldData = await schema
    .findOne({
      name: name,
    })
    .lean();
  if (oldData) {
    throw createError({
      statusCode: 409,
      statusMessage: "数据已存在，请修改后重试",
    });
  }
  const newData = await schema.create(result.data);
  return newData;
});
